package com.dss.code_generation.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.dss.code_generation.config.DssProperties;
import com.dss.code_generation.service.IYapiService;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Description:
 * @author: dongshoushan
 * @date: 2021年11月18日 15:53
 */
@Service
public class YapiServiceImpl implements IYapiService {
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private DssProperties dssProperties;
    @Override
    public JSONObject getByDto(String name,String dtoType) {
        String template = restTemplate.getForObject(dssProperties.getSwaggerApiUrl(), String.class);
        JSONObject jsonObject = JSONObject.parseObject(template);
        String  definitions = jsonObject.getString("definitions");
        JSONObject definitionsJsonObject = JSONObject.parseObject(definitions);
        String result = definitionsJsonObject.getString(name);
        if (result == null) {
            return null;
        }
        JSONObject propertiesObj = JSONObject.parseObject(result);
        String properties = propertiesObj.getString("properties");

        JSONObject jsonObject1 = JSONObject.parseObject(properties);
        Set<String> strings = jsonObject1.keySet();
        List<String> list = new ArrayList<>();
        strings.forEach(s -> list.add(s));
        for (String string : list) {
            String string1 = jsonObject1.getString(string);
            if (string1.contains("originalRef")){
                JSONObject jsonObject2 = JSONObject.parseObject(string1);
                String type = jsonObject2.getString("type");
                if ("array".equals(type)){
                    String items = jsonObject2.getString("items");
                    String originalRef = JSONObject.parseObject(items).getString("originalRef");
                    String result2 = definitionsJsonObject.getString(originalRef);

                    //处理一个类包含有另外类的场景
                    JSONObject jsonObject3 = JSONObject.parseObject(result2);
                    jsonObject2.put("items",jsonObject3);
                    jsonObject1.put(string,jsonObject2);
                }else {
                    String originalRef = jsonObject2.getString("originalRef");
                    String result2 = definitionsJsonObject.getString(originalRef);

                    //处理一个类包含有另外类的场景
                    JSONObject jsonObject3 = JSONObject.parseObject(result2);
                    jsonObject3.put("description",jsonObject2.getString("description"));
                    jsonObject1.put(string,jsonObject3);

                }
            }
        }
        JSONObject jsonObjectYapi = new JSONObject();
        if(StringUtils.isNotBlank(propertiesObj.getString("required"))){
            jsonObjectYapi.put("required",JSONObject.parseArray(propertiesObj.getString("required")));
        }
        JSONObject typeJson1 = new JSONObject();
        typeJson1.put("type","string");
        JSONObject typeJson2 = new JSONObject();
        typeJson2.put("type","number");
        JSONObject propertiesJson = new JSONObject();
        propertiesJson.put("code",typeJson2);
        propertiesJson.put("msg",typeJson1);

        switch (dtoType){
            //入参单个对象
            case "0":{
                jsonObjectYapi.put("properties",jsonObject1);
            }break;
            //出参 单个对象
            case "1":{
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("type","object");
                jsonObject2.put("properties",jsonObject1);

                propertiesJson.put("data",jsonObject2);
                jsonObjectYapi.put("properties",propertiesJson);
            }break;
            //出参List对象
            case "2":{
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("type","object");
                jsonObject2.put("properties",jsonObject1);
                JSONObject jsonObject3 = new JSONObject();
                jsonObject3.put("type","array");
                jsonObject3.put("items",jsonObject2);

                propertiesJson.put("data",jsonObject3);
                jsonObjectYapi.put("properties",propertiesJson);
            }break;
            //出参分页对象
            case "3":{
                propertiesJson.put("total",typeJson2);

                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("type","object");
                jsonObject2.put("properties",jsonObject1);
                JSONObject jsonObject3 = new JSONObject();
                jsonObject3.put("type","array");
                jsonObject3.put("items",jsonObject2);

                propertiesJson.put("rows",jsonObject3);
                jsonObjectYapi.put("properties",propertiesJson);
            }break;
            default :
                System.out.println("参数错误");
        }



        jsonObjectYapi.put("type","object");
        jsonObjectYapi.put("$schema","http://json-schema.org/draft-04/schema#");
        System.out.println("========获取返回结果拷贝Yapi=====");
        System.out.println(jsonObjectYapi);
        return jsonObjectYapi;
    }
}
